package cn.neusoft.luofa603.yuejian_29.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import cn.neusoft.luofa603.yuejian_29.Model;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.bean.UserLogin;
import cn.neusoft.luofa603.yuejian_29.databinding.ActivityLoginBinding;

public class LoginActivity extends AppCompatActivity {
    private SharedPreferences.Editor editor;
    private UserLogin loginResult;
    private String sessionid;
    private EditText tell,pwdd;
    CheckBox check;
    SharedPreferences su;
    ActivityLoginBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        binding.setListeners(new Listeners());
        su=getSharedPreferences("loginResult",MODE_PRIVATE);
        initviews();
        innitEvents();
             readSu();
    }

    public void initviews(){
        tell=(EditText)findViewById(R.id.tel);
        pwdd=(EditText)findViewById(R.id.pwd);
        check=(CheckBox)findViewById(R.id.check);
    }
    public void innitEvents(){
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (check.isChecked()) {
                    saveSu();
                } else {

                }
            }
        });
    }

//    check.setChecked(true);

    public class Listeners {
        public void onClickLogin(){
//
            if(binding.tel.getText().toString().equals("")){
                Toast.makeText(LoginActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();
            }else if (binding.pwd.getText().toString().equals("")){
                Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
            }else {
                    saveSu();
                loginResult=new UserLogin();

                Model.with(Model.userService.Login(binding.tel.getText().toString(), binding.pwd.getText().toString()))
                        .positive(new Model.Positive<UserLogin>() {
                            @Override
                            public void onPositive(UserLogin data) {
                                if (data.getUser_name() != null) {
                                    loginResult=data;
                                    sessionid=loginResult.getSessionid();
                                    saveSessionID(loginResult.getSessionid());
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                } else
                                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .request();
            }
        }
        public void onClickRegister(){
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivityForResult(intent,1);
            intent.getExtras();

        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data ){
        super.onActivityResult(requestCode,requestCode,data);
        if (requestCode==1){
            if(resultCode==RESULT_OK){
                String phone = data.getStringExtra("phone");
                String pwd = data.getStringExtra("pwd");
                binding.tel.setText(phone);
                binding.pwd.setText(pwd);
            }
        }
    }
    /**
     * 将用户名和密码存入SharedPreference
     * */
    public void saveSu(){
        SharedPreferences.Editor editor = su.edit();
//        String utel =  binding.tel.getText().toString();
        String utel= tell.getText().toString();
        editor.putString("user_name",utel);
//        String upwd = binding.pwd.getText().toString();
        String upwd = pwdd.getText().toString();
        editor.putString("password",upwd);
        //存储上次选中的状态：选还是未选
        if(check.isChecked())
            editor.putBoolean("checked",true);
        else
            editor.putBoolean("checked",false);
        editor.commit();
    }
    /**
     * 读取存入SharedPreferences里面的值
     * */
    public void readSu(){
        String utel = su.getString("user_name",null);
        String upwd = su.getString("password",null);
//        binding.tel.setText(utel);
//        /*String temp=binding.tel.getText().toString();
//        System.out.print("===================="+temp);*/
//        binding.pwd.setText(upwd);
        if(su.getBoolean("checked",false)) {
            tell.setText(utel);
            pwdd.setText(upwd);
            //只读上次选中状态
            check.setChecked(true);
        }
    }
    public void saveSessionID(String sessionid){
        SharedPreferences sharedPreferences = getSharedPreferences("sessionid",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("sessionid",sessionid);
        editor.apply();

    }
}
